// pages/my/myOrder/myOrder.js
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 接受响应值
    respList: [],
    // 搜索内容
    orderGoodsName: '',
    // 提示文本
    actionText: '',
    // 课程订单
    orderType: "",
    // 退款订单
    orderStatus: '',
    // 是否支付,数据库分析后改动此项
    payOrder: false,
    goodsImgUrl:'',
  },
  // 返回上一页
  onNavigate() {
    wx.navigateBack({
      delta: 1
    })
  },
  // 搜索框--赋值
  changeHandle(e) {
    this.setData({
      orderGoodsName: e.detail.value
    });
  },
  // 显示搜索按钮--无需关心
  focusHandle() {
    this.setData({
      actionText: '搜索',
    });
  },
  // 失去焦点后清空搜索内容
  blurHandle() {
    this.setData({
      actionText: '',
    });
  },
  // 点击清空按钮之后触发
  clearHandle() {
    this.setData({
      orderGoodsName: '',
    })
    this.getList()
  },
  // 触发搜索功能
  actionHandle() {
    console.log(this.data.orderGoodsName);
    this.getList()
  },

  // 滑动
  onChange(e) {
    wx.showToast({
      title: `切换到标签 ${e.detail.name}`,
      icon: 'none',
    });
    // 赋值
    if (e.detail.name == 0) {
      this.setData({
        // 课程订单
        orderType: "",
        // 退款订单
        orderStatus: '',
      })
      this.getList()

    } else if (e.detail.name == 1) {
      this.setData({
        // 课程订单
        orderType: 0,
        // 退款订单
        orderStatus: '',
      })
      this.getList()
    } else if (e.detail.name == 2) {
      this.setData({
        // 课程订单
        orderType: 1,
        // 退款订单
        orderStatus: '',
      })
      this.getList()

    } else if (e.detail.name == 3) {
      this.setData({
        // 课程订单
        orderType: 2,
        // 退款订单
        orderStatus: '',
      })
      this.getList()
    } else if (e.detail.name == 4) {
      this.setData({
        // 课程订单
        orderType: '',
        // 退款订单
        orderStatus: 4,
      })
      this.getList()
    }

  },

  // 查询订单请求--模糊查询
  getList() {
    wx.request({
      url: 'http://localhost:8080/keep/order/condition',
      method: 'GET',
      data: {
        orderGoodsName: this.data.orderGoodsName,
        orderType: this.data.orderType,
        orderStatus: this.data.orderStatus,
      },
      header: {
        'Authorization': getApp().globalData.authorization
      },
      success: resp => {
        this.setData({
          respList: resp.data.rows
        })
        this.data.respList.forEach(item => {
          if (item.goods != null) {
            // 图片路径赋值
            this.setData({
              goodsImgUrl: JSON.parse(item.goods.goodsPicture)[0].url
            })
          }
        });
        console.log(this.data.respList);
      }
    })
  },
  // 跳转详情页
  toDetails(e) {
      console.log(e.currentTarget.dataset.index);
    var orderDetail = JSON.stringify(e.currentTarget.dataset.index);
    if (e.currentTarget.dataset.index.orderType != 2) {
      wx.navigateTo({
        url: '/pages/my/orderDetail/orderDetail?e=' + orderDetail,
      })
    } else {
      wx.navigateTo({
        url: '/pages/my/myOrder/myOrderDetails/myOrderDetails?e=' + orderDetail,
      })
      console.log(222);
    }
  },
  refundHandle(e) {
    wx.showModal({
      title: '申请退款',
      content: '您确定要退款吗？',
      success: (res) => {
        if (res.confirm) {
          wx.request({
            url: 'http://localhost:8080/keep/order/updateOrderStatus',
            method: 'PUT',
            data: e.currentTarget.dataset.index,
            header: {
              'Authorization': getApp().globalData.authorization
            },
            success: resp => {
              if (resp.data.code == 200) {
                wx.showToast({
                  title: '申请退款成功，待退款中...',
                  icon: 'success',
                  duration: 2000
                })
                this.getList()
              } else {
                wx.showToast({
                  title: '申请退款失败',
                  icon: 'loading',
                  duration: 2000
                })
              }
            }
          })

        } else if (res.cancel) {
          return;
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})